package com.taobao.android.mnndemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class PoseView extends View {

    private float[] keypoints; // Store the keypoints here
    private Paint paint;

    public PoseView(Context context) {
        super(context);
        init();
    }

    public PoseView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setColor(Color.RED); // Set the color of the points
        paint.setStrokeWidth(10); // Set the size of the points
    }

    public void setKeypoints(float[] keypoints) {
        this.keypoints = keypoints;
        invalidate(); // Request to redraw the view with new keypoints
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (keypoints == null || keypoints.length % 3 != 0) {
            return; // If there are no keypoints or if the format is incorrect, do nothing
        }

        int width = getWidth();
        int height = getHeight();

        // Loop through the keypoints and draw them on the canvas
        for (int i = 0; i < keypoints.length; i += 3) {
            float x = keypoints[i] * width; // Map x from normalized to view coordinates
            float y = keypoints[i + 1] * height; // Map y from normalized to view coordinates
            Log.d(TAG, "onDraw() called with: x = [" + x + ", y = [" + y + "]");
            // Draw the keypoint
            canvas.drawCircle(x, y, 30, paint);
        }

        // Optionally, you can draw lines connecting keypoints (e.g., to form a skeleton)
        // For example, draw a line between keypoints[0] and keypoints[1]
        // You would need to define which keypoints to connect
//         canvas.drawLine(x1, y1, x2, y2, paint);
    }

    private static final String TAG = "PoseView";
}